<?php

namespace app\mobile\controller;

class Linesort extends Common {

        public function day() {
                $id = I("get.day");
                $navigation = explode("\n", C('site.navigation'));
                foreach ($navigation as $key => $value) {
                        $value = explode("|", $value);
                        $value[1] = str_replace(array("\r\n", "\r", "\n"), "", $value[1]);
                        $navigation[$key] = array('name' => $value[0], 'url' => $value[1]);
                }
                if ($id == 1) {
                        $this_zone['name'] = $navigation[0]['name'];
                } else if ($id == 2) {
                        $this_zone['name'] = $navigation[1]['name'];
                } else if ($id == 3) {
                        $this_zone['name'] = $navigation[2]['name'];
                } else if ($id == 4) {
                        $this_zone['name'] = $navigation[3]['name'];
                } else if ($id == 5) {
                        $this_zone['name'] = $navigation[4]['name'];
                }
                if (!empty($this_zone['name'])) {
                        $this_zone['name'] = $this_zone['name'];
                } else {
                        $this_zone['name'] = '';
                }
                $this_zone['write_string'] = 'yiriyou';

                $this->assign('this_zone', $this_zone);
                $condition = array();

                if (I("?get.type")) {
                        $type = I('get.type');
                } else {
                        $type = 0;
                }
                $condition['type'] = $type;

                if (I("?get.day")) {
                        $day = I('get.day');
                } else {
                        $day = 0;
                }
                $condition['day'] = $day;

                if (I("?get.label")) {
                        $label = I('get.label');
                } else {
                        $label = 0;
                }
                $condition['label'] = $label;

                if (I("?get.order")) {
                        $order = I('get.order');
                } else {
                        $order = 0;
                }
                $condition['order'] = $order;

                if (I("?get.sort")) {
                        $sort = I('get.sort');
                } else {
                        $sort = 0;
                }
                $condition['sort'] = $sort;

                if (I("?get.min_price")) {
                        $min_price = I('get.min_price');
                } else {
                        $min_price = 0;
                }
                $condition['min_price'] = $min_price;
                if (I("?get.max_price")) {
                        $max_price = I('get.max_price');
                } else {
                        $max_price = 0;
                }
                $condition['max_price'] = $max_price;
                $this->assign($condition);


                $data = array('ischecked' => 1);
                //筛选类型			
                $toursType = M("ToursType")->field(true)->cache()->select();
                $this->assign("types", $toursType);
                //玩法
                $labels_list = M("Tours")->field("labels")->group("labels")->where($data)->getField("labels", true);
                if (is_array($labels_list)) {
                        $labels_list = array_filter($labels_list);
                } else {
                        $labels_list = $labels_list;
                }
                $ids = '';
                if (is_array($labels_list)) {
                        foreach ($labels_list as $v) {
                                $ids .= $v;
                        }
                } else {
                        $labels = array();
                }
                $labels_id = array_unique(explode(",", $ids));
                foreach ($labels_id as $k => $v) {
                        if (!empty($v)) {
                                $foo = D("ToursLabel")->where(array("id" => $v))->find();
                                if (!empty($foo)) {
                                        $labels[$v] = $foo;
                                }
                        }
                }
                $this->assign("labels", $labels);
                //天数
                $trip_days = M("Tours")->field("trip_days")->where($data)->group("trip_days")->cache()->select();
                foreach ($trip_days as $key => $value) {
                        if ($value['trip_days'] == 0) {
                                unset($trip_days[$key]);
                        }
                }
                $this->assign("days", $trip_days);
                $Tours = D('tours/Tours');
                //推荐产品
                $where = array();
                $where['ischecked'] = 1;
                $where['isgood'] = 1;
                $commend = $Tours->relation(array('tours_price', 'tours_site'))->field(true)->where($where)->order('dateline desc')->limit(5)->select();
                $this->assign('commend', $commend);
                //产品类型
                //最新资讯
                $arclist = D("News")->where(array('ischecked' => 1, 'cid' => 1))->field(true)->order('postdate desc')->limit(8)->select();
                $this->assign("newslist", $arclist);
                $from = array('，', '{sname}', '{label}', '{day}', '{type}');
                $type_seo = '';
                if ($type > 0) {
                        if (!(M("ToursType")->field(true)->where(array('id' => $type))->cache()->getField('name'))) {
                                $type_seo = M("ToursType")->field(true)->where(array('id' => $type))->cache()->getField('name');
                        } else {
                                $type_seo = '';
                        }
                }
                $label_seo = '';
                if ($label > 0) {
                        if (!empty($labels[$label]['title'])) {
                                $label_seo = $labels[$label]['title'];
                        } else {
                                $label_seo = '';
                        }
                }
                //选择类型 已选条件
                $haveselect = array();
                if ($type > 0) {
                        $data['type'] = $type;
                        $haveselect['1']['name'] = M("ToursType")->field(true)->where(array('id' => $type))->cache()->getField('name');
                        $haveselect['1']['url'] = U('/' . $this_zone['write_string'] . '/0/' . $label . '/' . $day, '', '', false, true);
                }
                //选择玩法
                if ($label > 0) {
                        $data['labels'] = array("like", "%," . $label . ",%");
                        $haveselect['3']['name'] = $labels[$label]['title'];
                        $haveselect['3']['url'] = U("/" . $this_zone['write_string'] . '/' . $type . '/0' . '/' . $day, '', '', false, true);
                }

                //选择天数
                if ($day > 0) {
                        $data['trip_days'] = $day;
                        $haveselect['2']['name'] = $day . '日游';
                        $haveselect['2']['url'] = U('/' . $this_zone['write_string'] . '/' . $type . '/' . $label . '/' . '0', '', '', false, true);
                }
                $this->assign('haveselect', $haveselect);
                //价格
                if ($min_price > 0) {
                        $data['price'][] = array('gt', $min_price);
                }
                if ($max_price > 0) {
                        $data['price'][] = array('lt', $max_price);
                }
                //排序
                $sort_s = ($sort == 1) ? "desc" : "asc";
                switch ($order) {
                        case 1:
                                //推荐
                                $order_s = "isgood " . $sort_s;
                                break;
                        case 2:
                                //特惠
                                $order_s = "ispreference " . $sort_s;
                                break;
                        case 3:
                                //点击
                                $order_s = "guanzhu " . $sort_s;
                                break;
                        case 4:
                                //价格
                                $order_s = "price " . $sort_s;
                                break;
                        default:
                                $order_s = "dateline desc ";
                }

                //数据开始
                $count = $Tours->where($data)->count();
                $url = $this_zone['write_string'] . '/';
                $page = new \org\Page($count, 8, '', $url);

                $lines_list = $Tours->relation(array('tours_price', 'tours_site'))->field(true)->where($data)->order($order_s)->limit($page->firstRow, $page->listRows)->select();
                $this->assign('lines_list', $lines_list);
                $this->assign('page', $page->show());



                $from = array('，', '{sname}', '{label}', '{day}', '{type}');
                $to = array(',', $this_zone['name'], $label_seo, ($day > 0) ? $day . "日游" : "", $type_seo);
                $seo_title = str_replace($from, $to, C('site.linesort_seotitle'));
                $seo_keywords = str_replace($from, $to, C('site.linesort_seokeywords'));
                $seo_description = str_replace($from, $to, C('site.linesort_seodescription'));
                $this->assign('seo_title', $seo_title);
                $this->assign('seo_keywords', $seo_keywords);
                $this->assign('seo_description', $seo_description);
                return $this->fetch('show_list');
        }

        public function search_price() {
                if (IS_POST) {
                        $write_string = $_REQUEST['write_string'];
                        $url_type = $_REQUEST['type'] ? $_REQUEST['type'] : 0;
                        $url_day = $_REQUEST['day'] ? $_REQUEST['day'] : 0;
                        $url_label = $_REQUEST['label'] ? $_REQUEST['label'] : 0;
                        $url_min_price = is_numeric($_POST['min_price']) ? $_POST['min_price'] : 0;
                        $url_max_price = is_numeric($_POST['max_price']) ? $_POST['max_price'] : 0;
                        $this->redirect(U('/' . $write_string . '/' . $url_type . '/' . $url_label . '/' . $url_day . '/' . $url_min_price . '/' . $url_max_price . '/0/0'));
                } else {
                        $this->_empty();
                }
        }

        public function mudidi() {
                $zone2 = D('tours/Location');
                // $daozone_list = implode(',', $daozone);
                $where['status'] = 1;
                $where['zhuozhong'] =1;
                $zonelist1 = $zone2->where($where)->cache()->select();
                foreach ($zonelist1 as $k => $v) {
                        $zonelist1[$k]['xianlu'] = M('Tours')->where(array('cid' => array('like', '%,' . $v['id'] . ',%'), 'ischecked' => '1'))->cache()->count();
                }
                $this->assign('zone', $zonelist1);
                return $this->fetch();
        }

        public function line_show() {
                $id = I("get.id");
                //面包削
                $this_zone = M('Location')->where(array('id' => $id))->cache()->find();
                $this->assign('this_zone', $this_zone);
                //父级
                //获得过滤级
                $pt_ids = M('Location')->where(['pid' => ['in', '1,2']])->field('id')->select();
                $vv = '';
                foreach ($pt_ids as $v) {
                        $vv = $vv . ',' . $v['id'];
                }
                $arr_nd = explode(',', $vv);
                $zone = '';
                if ($this_zone['pid'] == 1 || $this_zone['pid'] == 2 || in_array($this_zone['pid'], $arr_nd)) {
                        
                } else {
                        $zone = M('Location')->where(array('id' => $this_zone['pid']))->cache()->find();
                        $zone['channel_url'] = U('/' . $zone['write_string'] . '@www', '', '', false, false, true);
                        $zone['channel_name'] = $zone['name'];
                }
                $this->assign('zone', $zone);

                $condition = array();

                if (I("?get.type")) {
                        $type = I('get.type');
                } else {
                        $type = 0;
                }
                $condition['type'] = $type;

                if (I("?get.day")) {
                        $day = I('get.day');
                } else {
                        $day = 0;
                }
                $condition['day'] = $day;

                if (I("?get.label")) {
                        $label = I('get.label');
                } else {
                        $label = 0;
                }
                $condition['label'] = $label;

                if (I("?get.order")) {
                        $order = I('get.order');
                } else {
                        $order = 0;
                }
                $condition['order'] = $order;

                if (I("?get.sort")) {
                        $sort = I('get.sort');
                } else {
                        $sort = 0;
                }
                $condition['sort'] = $sort;

                if (I("?get.min_price")) {
                        $min_price = I('get.min_price');
                } else {
                        $min_price = 0;
                }
                $condition['min_price'] = $min_price;
                if (I("?get.max_price")) {
                        $max_price = I('get.max_price');
                } else {
                        $max_price = 0;
                }
                $condition['max_price'] = $max_price;
                $this->assign($condition);


                $data = array('cid' => array('like', "%," . $id . ",%"), 'ischecked' => 1);
                //筛选类型			
                $toursType = M("ToursType")->field(true)->cache()->select();
                $this->assign("types", $toursType);
                //玩法
                $labels_list = M("Tours")->field("labels")->group("labels")->where($data)->getField("labels", true);
                $labels = array();
                if (is_array($labels_list)) {
                        $labels_list = array_filter($labels_list);
                        $ids = '';
                        foreach ($labels_list as $v) {
                                $ids .= $v;
                        }
                        $labels_id = array_unique(explode(",", $ids));
                        foreach ($labels_id as $k => $v) {
                                if (!empty($v)) {
                                        $foo = D("ToursLabel")->where(array("id" => $v))->find();
                                        if (!empty($foo)) {
                                                $labels[$v] = $foo;
                                        }
                                }

                                $this->assign("labels", $labels);
                        }
                } else {
                        $labels_list = $labels_list;
                        $foo = D("ToursLabel")->where(array("id" => $labels_list))->find();
                        if (!empty($foo)) {
                                $labels[$labels_list] = $foo;
                        }

                        $this->assign("labels", $labels);
                }

                //天数
                $trip_days = M("Tours")->field("trip_days")->where($data)->group("trip_days")->cache()->select();
                foreach ($trip_days as $key => $value) {
                        if ($value['trip_days'] == 0) {
                                unset($trip_days[$key]);
                        }
                }
                $this->assign("days", $trip_days);
                $Tours = D('tours/Tours');
                //推荐产品
                $where = array();
                $where['ischecked'] = 1;
                $where['cid'] = array('like', "%," . $id . ",%");
                $where['isgood'] = 1;
                $commend = $Tours->relation(array('tours_price', 'tours_site'))->field(true)->where($where)->order('dateline desc')->limit(5)->select();
                $this->assign('commend', $commend);
                //产品类型
                //最新资讯
                $arclist = D("News")->where(array('ischecked' => 1, 'cid' => 1))->field(true)->order('postdate desc')->limit(8)->select();
                $this->assign("newslist", $arclist);
                $from = array('，', '{sname}', '{label}', '{day}', '{type}');
                $type_seo = '';
                if ($type > 0) {
                        if (!empty($toursType[$type]['name'])) {
                                $type_seo = $toursType[$type]['name'];
                        } else {
                                $type_seo = '';
                        }
                }
                $label_seo = '';
                if ($label > 0) {
                        if (!(M("ToursType")->field(true)->where(array('id' => $type))->cache()->getField('name'))) {
                                $label_seo = M("ToursType")->field(true)->where(array('id' => $type))->cache()->getField('name');
                        } else {
                                $label_seo = '';
                        }
                }
                //选择类型 已选条件
                $haveselect = array();
                if ($type > 0) {
                        $data['type'] = $type;
                        $haveselect['1']['name'] = M("ToursType")->field(true)->where(array('id' => $type))->cache()->getField('name');
                        $haveselect['1']['url'] = U('/' . $this_zone['write_string'] . '/0/' . $label . '/' . $day, '', '', false, true);
                }
                //选择玩法
                if ($label > 0) {
                        $data['labels'] = array("like", "%," . $label . ",%");
                        $haveselect['3']['name'] = $labels[$label]['title'];
                        $haveselect['3']['url'] = U("/" . $this_zone['write_string'] . '/' . $type . '/0' . '/' . $day, '', '', false, true);
                }

                //选择天数
                if ($day > 0) {
                        $data['trip_days'] = $day;
                        $haveselect['2']['name'] = $day . '日游';
                        $haveselect['2']['url'] = U('/' . $this_zone['write_string'] . '/' . $type . '/' . $label . '/' . '0', '', '', false, true);
                }
                $this->assign('haveselect', $haveselect);
                //价格
                if ($min_price > 0) {
                        $data['price'][] = array('gt', $min_price);
                }
                if ($max_price > 0) {
                        $data['price'][] = array('lt', $max_price);
                }
                //排序
                $sort_s = ($sort == 1) ? "desc" : "asc";
                switch ($order) {
                        case 1:
                                //推荐
                                $order_s = "isgood " . $sort_s;
                                break;
                        case 2:
                                //特惠
                                $order_s = "ispreference " . $sort_s;
                                break;
                        case 3:
                                //点击
                                $order_s = "guanzhu " . $sort_s;
                                break;
                        case 4:
                                //价格
                                $order_s = "price " . $sort_s;
                                break;
                        default:
                                $order_s = "dateline desc ";
                }

                //数据开始
                $count = $Tours->where($data)->count();
                $url = $this_zone['write_string'] . '/';
                $page = new \org\Page($count, 8, '', $url);

                $lines_list = $Tours->relation(array('tours_price', 'tours_site'))->field(true)->where($data)->order($order_s)->limit($page->firstRow, $page->listRows)->select();
                $this->assign('lines_list', $lines_list);
                $this->assign('page', $page->show());


                $from = array('，', '{sname}', '{label}', '{day}', '{type}');
                $to = array(',', $this_zone['name'], $label_seo, ($day > 0) ? $day . "日游" : "", $type_seo);
                $seo_title = str_replace($from, $to, C('site.linesort_seotitle'));
                $seo_keywords = str_replace($from, $to, C('site.linesort_seokeywords'));
                $seo_description = str_replace($from, $to, C('site.linesort_seodescription'));
                $this->assign('seo_title', $seo_title);
                $this->assign('seo_keywords', $seo_keywords);
                $this->assign('seo_description', $seo_description);
                return $this->fetch('show_list');
        }

        public function search_key() {
                $data = array();
                $data['isdelete'] = "0";
                $data['ischecked'] = "1";
                if (I('get.searchid')) {

                        //搜索行程
                        if ($ad['minday'] && $ad['maxday']) {
                                $minday = $ad['minday'];
                                $maxday = $ad['maxday'];
                                $data['trip_days'] = array(array('egt', $minday), array('elt', $maxday));
                        } elseif ($ad['minday']) {
                                $minday = $ad['minday'];
                                $data['trip_days'] = array('egt', $minday);
                        } elseif ($ad['maxday']) {
                                $maxday = $ad['maxday'];
                                $data['trip_days'] = array('elt', $maxday);
                        }

                        //搜索价格
                        if ($ad['minprice'] && $ad['maxprice']) {
                                $minprice = $ad['minprice'];
                                $maxprice = $ad['maxprice'];
                                $data['price'] = array(array('egt', $minprice), array('elt', $maxprice));
                        } elseif ($ad['minprice']) {
                                $minprice = $ad['minprice'];
                                $data['price'] = array('egt', $minprice);
                        } elseif ($ad['maxprice']) {
                                $maxprice = $ad['maxprice'];
                                $data['price'] = array('elt', $maxprice);
                        }

                        //搜索性质quality
                        if ($ad['quality']) {
                                $quality = $ad['quality'];
                                $data['type'] = $quality;
                        }
                        //搜索主题 theme_id
                        if ($ad['tid']) {
                                $theme_id = $ad['tid'];
                                $data['theme_id'] = $theme_id;
                        }
                } elseif (I('get.keyword')) {
                        if (is_gb2312(I('get.keyword'))) {
                                $keyword = iconv('gb2312', 'utf-8//IGNORE', I('get.keyword'));
                        } else {
                                $keyword = I('get.keyword');
                        }
                        $keyword = addslashes($keyword);
                        $url = "search/" . $keyword . '/';
                        $search_log = M("SearchLog");
                        if ($keyword && (strlen($keyword) > 2)) {
                                $log['keyword'] = $keyword;
                                $log['ip'] = get_client_ip();
                                $log['dateline'] = time();
                                $search_log->data($log)->add();
                        }
                        $keywords = str_replace("，", ",", $keyword);
                        $keywords = explode(",", $keywords);
                        foreach ($keywords as $value) {
                                if ($value) {
                                        $where_k[] = " title LIKE '%" . $value . "%' ";
                                        $where_k[] = " sight_cache LIKE '%" . $value . "%' ";
                                }
                        }
                        $search_title = $keyword;
                        $data['_string'] = implode(" OR ", $where_k);
                } else {
                        return $this->error("请输入搜索关键词！");
                }

                $Tours = D("tours/Tours");

                $count = $Tours->where($data)->count();
                $page = new \org\Page($count, 30, "", $url);

                $lines_list = $Tours->field(true)->relation('tours_trip')->where($data)->order('dateline desc')->limit($page->firstRow, $page->listRows)->select();

                if ($lines_list !== false) {
                        $this->assign('page', $page->show());
                        $this->assign('lines_list', $lines_list);
                }
                $this->assign('search_title', $search_title);
                $this->assign('title_text', '');

                //特惠线路
                $hotList = $Tours->field(true)->where(array('ispreference' => 1))->order('dateline desc')->limit(6)->select();
                $this->assign('hotList', $hotList);

                //SEO优化，生成TITLE,K,D
                $from = array('，', '{keyword}');
                $to = array(',', $search_title);

                $seo_title = str_replace($from, $to, C('site.search_seotitle'));
                $seo_keywords = str_replace($from, $to, C('site.search_seokeywords'));
                $seo_description = str_replace($from, $to, C('site.search_seodescription'));
                $this->assign('seo_title', $seo_title);
                $this->assign('seo_keywords', $seo_keywords);
                $this->assign('seo_description', $seo_description);

                return $this->fetch("linesort/show_search");
        }

}
